<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class ImageController extends HomeController
{

    public function _initialize(){
        parent::_initialize();
        $this->is_login();      //判断是否登录
    }

    //问题列表
    public function get_list(){
        $requestdata = file_get_contents("php://input");
        $paramData = json_decode($requestdata, true);
        $model = D("Image");
        $field = "a.*,a.id as image_id,DATE_FORMAT(FROM_UNIXTIME(a.create_time), '%Y-%m-%d %H:%i') as create_date";
        $result = $model->get_image_list($paramData,$field);
        if($result['list']) {
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            foreach($result['list'] as $key=>$val){
                $val['image'] = [['url'=>$protocol.$this->img_url.$val['image']]];
                $result['list'][$key] = $val;
            }
        }

        $returnData = [
            'code' => 200,
            'data' => $result['list']?$result['list']:[],
            'total' => $result['count']
        ];
        echo json_encode($returnData);
    }


    //添加/修改问题
    public function save(){
        $requestdata = file_get_contents("php://input");
        $paramData = json_decode($requestdata, true);
        $model = D("Image");
        if(empty($paramData['image'])) $this->throw_exception('图片不能为空');
        $paramData['userId'] =  $this->user_info['id'];
        $result = $model->image_save($paramData);
        $this->throw_exception($result['msg'],$result['code']);
    }



}
